package com.example.service;

import com.example.entity.Film;
import com.example.mapper.CommentMapper;
import com.example.mapper.FilmMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class FilmService {
    //注入Mapper
    @Resource
    private FilmMapper filmMapper;
    @Resource
    private CommentMapper commentMapper;
    /**
     * 新增
     * */
    public void add(Film film) {
        filmMapper.insert(film);
    }
    /**
     * 更新
     * */
    public void update(Film film) {
        filmMapper.updateById(film);
    }
    /**
     * 删除
     * */
    public void deleteById(Integer id) {
        filmMapper.deleteById(id);
    }
    /**
     * 查询所有
     * */
    public List<Film> selectAll(Film film) {
        return filmMapper.selectAll(film);
    }
    /**
     * 分页查询
     *  @param pageNum 页码
     *  @param pageSize 每页显示数量
     * @return  分页查询结果 返回Film对象
     * */
    public PageInfo<Film> selectPage(Film film, Integer pageNum, Integer pageSize) {
        //分页查询
        PageHelper.startPage(pageNum, pageSize);
        List<Film> list = filmMapper.selectAll(film);
        //遍历查询结果，设置总页数和总记录数
        for (Film f : list) {
            //查询当前电影的评论数
            this.setScore(f);
        }
        return PageInfo.of(list);
    }
/**
 * 根据id查询
 * @param id 电影id
 * @return 电影对象
 * */

    public Film selectById(Integer id) {
        Film f = filmMapper.selectById(id);
        this.setScore(f);
        return f;
    }
/**
 * 推荐电影
 * @return 推荐电影列表
 * */
    public List<Film> selectRecommend(Integer filmId) {
        List<Film> films = this.selectAll(null);
        //过滤掉自己
        films = films.stream().filter(film -> !film.getId().equals(filmId)).collect(Collectors.toList());
        Collections.shuffle(films);//随机排序
        List<Film> filmList = films.stream().limit(3).collect(Collectors.toList());
        for (Film film : filmList) {
            this.setScore(film);
        }
        return filmList;

    }
    //封装方法，计算平均分
 private void setScore(Film film) {
     int total =  commentMapper.setTotal(film.getId());
     film.setCommentNum(total);
     if (total == 0){
         film.setScore(0D);
     }else{
         //计算平均分
         double sum = commentMapper.setSum(film.getId());
         BigDecimal score = BigDecimal.valueOf(sum).divide(BigDecimal.valueOf(total), 1, BigDecimal.ROUND_HALF_UP);
         film.setScore(score.doubleValue());
     }
 }
    /**
     * 查询排行榜
     * @return 排行榜列表
     * */
    public List<Film> selectRanking() {
        List<Film> filmList = filmMapper.selectAll(null);
        //按照评分排序
        for (Film film : filmList){
            setScore(film);
        }
        //按照评分排序
        return filmList.stream().sorted((f1, f2) -> Double.compare(f2.getScore(), f1.getScore())).limit(5).toList();
    }


}